package com.mijie.homi.service.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.homi.dao.commom.CommentDao;
import com.mijie.homi.dao.commom.ReviewDao;
import com.mijie.homi.domain.commom.Comment;
import com.mijie.homi.service.common.ReplyService.REPLY;

@Service
public class CommentService {

	@Autowired
	private CommentDao dao;
	@Autowired
	private ReviewDao reviewDao;
	
	private Logger logger = Logger.getLogger(CommentService.class);
	
	public static enum COMMENT{
		MOOD,PHOTO,TOPIC_OPINION
	}
	
	public static Map<COMMENT,String> commentTableMap ;
	public static Map<COMMENT,String> tableMap ;
	public static Map<REPLY,String> replyTableMap ;
	
	static{
		commentTableMap = new HashMap<COMMENT,String>();
		commentTableMap.put(COMMENT.MOOD,"moodComment");
		commentTableMap.put(COMMENT.PHOTO,"photoComment");
		commentTableMap.put(COMMENT.TOPIC_OPINION,"topicOpinionComment");
		
		replyTableMap = new HashMap<REPLY,String>();
		replyTableMap.put(REPLY.MOOD_COMMENT,"moodComment");
		replyTableMap.put(REPLY.PHOTO_COMMENT,"photoComment");
		replyTableMap.put(REPLY.TOPIC_OPINION_COMMENT,"topicOpinionComment");
		
		tableMap = new HashMap<COMMENT,String>();
		tableMap.put(COMMENT.MOOD,"mood");
		tableMap.put(COMMENT.PHOTO,"photo");
		tableMap.put(COMMENT.TOPIC_OPINION,"topicOpinion");
	}
	
	public Comment getCommentById(Enum<REPLY> type,long id){
		return dao.getCommentById(replyTableMap.get(type), id);
	}
	
	public int increNumOfReply(Enum<REPLY> type,long targetId){
		return dao.increNumOfReply(replyTableMap.get(type), targetId);
	}
	
	public int decreNumOfReply(Enum<REPLY> type,long targetId){
		return dao.decreNumOfReply(replyTableMap.get(type), targetId);
	}

	public void increNumOfComment(Enum<COMMENT> type,long targetId){
		dao.increNumOfComment(tableMap.get(type), targetId);
	}
	
	public void postComment(Enum<COMMENT> type,Comment comment){
		comment.setIdx(Long.MAX_VALUE-System.currentTimeMillis());
		comment.setTableName(commentTableMap.get(type));
		dao.postComment(comment);
	}
	
	public void decreNumOfComment(Enum<COMMENT> type,long targetId){
		dao.decreNumOfComment(tableMap.get(type), targetId);
	}
	
	public Comment getCommentById(String tableName,long id){
		return dao.getCommentById(tableName, id);
	}
	
	public void deleteComment(Enum<COMMENT> type,int reviewer,long id,long parentId){
		dao.deleteComment(commentTableMap.get(type), reviewer, id);
	}
	
	public void deleteCommentByAdmin(Enum<COMMENT> type,int administrator,long id,long parentId){
		dao.deleteCommentByAdmin(commentTableMap.get(type), administrator, id);;
	}
	
	public List<Comment> listComment(Enum<COMMENT> type,long idx,long targetId,int limit){
		return dao.listComment(commentTableMap.get(type),idx, targetId, limit);
	}
}
